package com.example.cargetter.mqtt;

import com.example.cargetter.pojo.CarData;
import com.example.cargetter.pojo.WarnFlag;
import com.example.cargetter.socket.UDPSend;
import com.example.cargetter.utils.AntherCarDataUtils;
import com.example.cargetter.utils.MqttDataUtils;
import com.example.cargetter.utils.WarnUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class PushCallback implements MqttCallback {
    @Autowired
    private MqttPushClient mqttPushClient;

    public static Map<String ,String> mp = new HashMap<>() ;

    @Autowired
    private MqttConfiguration mqttConfiguration;

    @Override
    public void connectionLost(Throwable cause) {
        log.info("连接断开，正在重连");
        MqttPushClient mqttPushClient = mqttConfiguration.getMqttPushClient();
        if (null != mqttPushClient) {
            mqttPushClient.connect(mqttConfiguration.getHost(), mqttConfiguration.getClientid(), mqttConfiguration.getUsername(),
                    mqttConfiguration.getPassword(), mqttConfiguration.getTimeout(), mqttConfiguration.getKeepalive());
            log.info("已重连");
        }

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {

    }

    @Override
    public void messageArrived(String topic, MqttMessage message)   {

        String res = new String(message.getPayload());
        log.info("接收消息主题 : " + topic);
        log.info("接收消息Qos : " + message.getQos());
        log.info("接收消息内容 : " + new String(message.getPayload()));

        try{
            //"test/publish"主题的信息处理
            if(topic.equals("test/publish")) {
                //如果此条mqtt信息是酒驾相关信息
                if(res.contains("mq3")) {
                    //添加时间数据，方便存储和判断重复
                    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    res += sdf.format(System.currentTimeMillis());
                    System.out.println("酒精数据，后端发送的是"+res);
                    UDPSend udpSend = new UDPSend();
                    udpSend.sendMessage(res);
                } else {
                    //是红绿灯数据,保存数据，供北向进行查询。
                    MqttDataUtils.getInstance().put(topic,new String(message.getPayload()));

                }

                //"car_data"主题的信息处理
            } else if(topic.equals("car_data")) {
                //接收另一小车处理数据后返回的警示结果，为一string的数字flag。返回给arkts修改UI。
                //存储另一辆小车的数据。为了保证进行判断时，本车数据已收到GPS数据。需要暂时存储另车数据，知道本车已有数据时进行判断。
                AntherCarDataUtils.getInstance().put(topic,new String(message.getPayload()));

//                WarnFlag warnFlag = WarnFlag.getInstance();

                //发送mqtt信息给另一小车：是否进行碰撞预警的信息。
                MqttSubClient mqttSubClient = new MqttSubClient(mqttPushClient);
//                mqttPushClient.publish("test/publish","warnFlag="+warnFlag.getFlag());
            }








        }catch (Exception e) {
//            e.printStackTrace();
//            log.info("222222222222");

        }

    }

}